package com.dj.uphome.web.service.system.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dj.common.web.service.impl.BaseCrudServiceImpl;
import com.dj.common.web.vo.PageAndSortVo;
import com.dj.uphome.web.mapper.system.SysRoleMapper;
import com.dj.uphome.web.service.system.SysRoleService;
import com.dj.uphome.web.vo.system.SysRoleVo;

@Service
public class SysRoleServiceImpl extends BaseCrudServiceImpl<SysRoleVo> implements SysRoleService {

	@Autowired
	private SysRoleMapper sysRoleMapper;

	@Override
	@Transactional
	public SysRoleVo create(SysRoleVo vo) throws Exception {
		if (vo.getRoleId() > 0) {
			sysRoleMapper.update(vo);
		} else {
			sysRoleMapper.save(vo);
		}
		return vo;
	}

	@Override
	public Page<SysRoleVo> findPage(Pageable pageRequest, SysRoleVo vo) throws Exception {

		PageAndSortVo<SysRoleVo> pageVo = convertToPageAndSortVo(pageRequest, vo, true);
		List<SysRoleVo> contentList = sysRoleMapper.findAll(pageVo);
		return new PageImpl<>(contentList, pageRequest, pageVo.getTotalResult());
	}

	@Override
	@Transactional
	public void delete(int id) throws Exception {
		sysRoleMapper.deleteById(id);
	}

	@Override
	public SysRoleVo findOne(int id) throws Exception {
		return sysRoleMapper.findById(id);
	}

	@Override
	public List<SysRoleVo> findAll(SysRoleVo vo) throws Exception {
		PageAndSortVo<SysRoleVo> pageVo = convertToPageAndSortVo(null, vo, false);
		return sysRoleMapper.findAll(pageVo);
	}

}
